package com.sunmouse.toolkit.widget.canvas;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/10/16 0016.
 */
public class PathTextView extends View {
    public static final String TAG = PathTextView.class.getSimpleName();
    private Paint mPaint;
    private Path path;
    private String src;
    private float strokeWidth;

    public PathTextView(Context context) {
        this(context, null);
    }

    public PathTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.strokeWidth = 10;
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(strokeWidth);
        mPaint.setTextSize(400);
        mPaint.setColor(Color.argb(255, 255, 0, 0));

        path = new Path();

        src = "A";

        this.mPaint.setPathEffect(selectPathEffect(0));
//        SpannableString ss = new SpannableString(src);
//        ss.setSpan(this, 0, src.length(), 0);
    }

    /**
     * path effect factory
     *
     * @param i
     * @return
     */
    private PathEffect selectPathEffect(int i) {
        PathEffect effect;
        switch (i) {
            case 0:
                effect=new DashPathEffect(new float[]{strokeWidth*3,strokeWidth},0);
                break;
            case 1:
                effect=new DashPathEffect(new float[]{strokeWidth*3,strokeWidth},0);
                break;
            default:
                effect=new DashPathEffect(new float[]{strokeWidth*3,strokeWidth},0);
                break;
        }
        return effect;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        path.reset();
        mPaint.getTextPath(src, 0, 1, getWidth() / 2, getHeight() / 2, path);
        path.close();

        canvas.translate(this.mPaint.getStrokeWidth() / 2, 0);
        canvas.drawPath(path, mPaint);
        canvas.translate(-this.mPaint.getStrokeWidth() / 2, 0);

    }
}
